summaryrefslogtreecommitdiff
path: root/src/scriptengine.cpp
blob: e4c5c2e125f85ec07f211f8f547c584770e6d1c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "scriptengine.h"
#include "lexer.h"
#include "parser.h"
#include "expression.h"

#include <bu/staticmembuf.h>
#include <bu/exceptionbase.h>
#include <bu/sio.h>

#include <stdlib.h>

ScriptEngine::ScriptEngine() :
    bRunning( true )
{
}

ScriptEngine::~ScriptEngine()
{
}

void ScriptEngine::exec( const Bu::String &sExpr )
{
    Bu::StaticMemBuf mb( sExpr.getStr(), sExpr.getSize() );
    exec( sExpr );
}

void ScriptEngine::exec( Bu::Stream &sInput )
{
    Lexer l( sInput );
    Parser p( l );
    Expression *pExp = p.parse();
    exec( pExp );
    delete pExp;
}

void ScriptEngine::exec( Expression *pExpr )
{
    NumStack sNums;

    for( Expression::iterator i = pExpr->begin(); i; i++ )
    {
//        Bu::sio << (*i).eType << " - " << sNums << Bu::sio.nl;
        if( ((*i).eType&Token::mMetaCmd) )
        {
            command( i );
            continue;
        }
        switch( (*i).eType )
        {
            case Token::tNumber:
                sNums.push( *(*i).nVal );
                break;

            case Token::tVariable:
                if( hVarState.has( *(*i).sVal ) )
                {
                    sNums.push( hVarState[*(*i).sVal] );
                }
                else
                {
                    if( sigError.isSet() )
                        sigError(
                            Bu::String("No such variable: $%1").
                                arg(*(*i).sVal)
                            );
                    return;
                }
                break;

            case Token::tPlus:
                {
                    Number b = sNums.peekPop();
                    Number a = sNums.peekPop();
                    sNums.push( a + b );
                }
                break;

            case Token::tMinus:
                {
                    Number b = sNums.peekPop();
                    Number a = sNums.peekPop();
                    sNums.push( a - b );
                }
                break;

            case Token::tDivide:
                {
                    Number b = sNums.peekPop();
                    Number a = sNums.peekPop();
                    sNums.push( a / b );
                }
                break;

            case Token::tMultiply:
                {
                    Number b = sNums.peekPop();
                    Number a = sNums.peekPop();
                    sNums.push( a * b );
                }
                break;

            case Token::tNegate:
                sNums.push( -sNums.peekPop() );
                break;
        }
    }

    if( sigNumResult.isSet() )
        sigNumResult( sNums.peekPop() );
}

void ScriptEngine::command( Expression::iterator &i )
{
    switch( (*i).eType )
    {
        case Token::tCmdExit:
            bRunning = false;
            return;

        case Token::tCmdScale:
            {
                if( !(++i) )
                {
                    if( sigError.isSet() )
                        sigError("You must provide a positive integer.");
                    return;
                }
                int32_t iScale = strtol( (*i).sVal->getStr(), 0, 10 );
                if( iScale < 0 )
                {
                    if( sigError.isSet() )
                        sigError("You must provide a positive integer.");
                    return;
                }
                if( sigMessage.isSet() )
                    sigMessage(Bu::String("Changed scale to: %1").arg( iScale ));
                for( VarHash::iterator i = hVarState.begin(); i; i++ )
                    (*i).setScale( iScale );
            }
            break;

        case Token::tCmdRadix:
            break;

        case Token::tCmdExtended:
            /*
    else if( sCmd == "vars" )
    {
    }
    else if( sCmd == "help" || sCmd == "?" )
    {
    }
    else
    {
    }*/
            break;
    }
}